package com.nl.controller.web.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageInfo;
import com.nl.controller.common.annotation.SystemServiceLog;
import com.nl.controller.common.service.CommonService;
import com.nl.controller.common.util.AES;
import com.nl.controller.web.bean.CheckMgr;
import com.nl.controller.web.bean.CheckNum;
import com.nl.controller.web.bean.CheckNumState;
import com.nl.controller.web.global.GlobalConst;
import com.nl.controller.web.mapper.CheckNumMapper;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class CheckNumService {

	@Autowired
	private CommonService commonService;

	@Autowired
	private CheckNumMapper checkNumMapper;

	@SystemServiceLog(value = false, description = "定位号码")
	public PageInfo<CheckNum> checkNum(String cityId,String mgrId,String decMsisdn,String mgrBrief,String startTime,String endTime){
		CheckNum checkNum = new CheckNum();
		checkNum.setCityId(cityId);
		String mgrName = checkNumMapper.findMgrName(mgrId);
		checkNum.setName(mgrName);
		checkNum.setMsisdn(decMsisdn);
		List<CheckNum> list = new ArrayList<CheckNum>();
		String yes = "是";
		String no ="否";
		String msisdn = decMsisdn;
		try {
			msisdn = AES.encrypt(decMsisdn, GlobalConst.SKEY);
		} catch (Exception e) {
			log.error("encrypt msisdn failed");
			e.printStackTrace();
		}
		CheckNumState checkNumState = checkNumMapper.findstate(mgrId,mgrBrief);
		checkNum.setState(checkNumState.getState());
		checkNum.setTemplateState(checkNumState.getTemplateState());
		checkNum.setIsLocal(checkNumState.getIsLocal());
		String countyName = checkNumState.getCountyName() == null ? "-" : checkNumState.getCountyName();
		checkNum.setCountyName(countyName);
		int stationNum = checkNumMapper.findStationNum(mgrId);
		checkNum.setStationNum(stationNum);	
		checkNum.setBelong(checkNum.getCityName()+"/"+countyName);
		
		int his = checkNumMapper.findHis(msisdn, mgrId);
		if (his == 0) {
			checkNum.setDataSources(no);
		} else {
			checkNum.setDataSources(yes);
			int iscz = checkNumMapper.findIscz(msisdn, mgrId);
			if (iscz == 0) {
				checkNum.setDataSources(no);
			} else {
				checkNum.setDataSources(yes);
			}
		}
		
		int seven = checkNumMapper.findSeven(msisdn, mgrId);
		String sendTime = checkNumMapper.findSevenSendTime(decMsisdn, mgrId);
		sendTime = sendTime==null?"无":sendTime;
		if (seven == 0) {
			checkNum.setIsSeven(no+"("+sendTime+")");
		}else {
			checkNum.setIsSeven(yes+"("+sendTime+")");
		}
		int black = checkNumMapper.findBlack(msisdn, cityId);
		if (black==0) {
			checkNum.setIsBlack(no);
		}else {
			checkNum.setIsBlack(yes);
		}
		int smSend = checkNumMapper.findSmSend(msisdn, mgrId);
		if (smSend==0) {
			checkNum.setSmSend(no);
		} else {
			checkNum.setSmSend(yes);
			checkNum.setResult(checkNumMapper.findResult(msisdn, mgrId));
		}
		int detail = checkNumMapper.findDetail(decMsisdn, mgrId, startTime, endTime);
		if (detail==0) {
			checkNum.setDeail(no);
		} else {
			checkNum.setDeail(yes);
			checkNum.setSendState(checkNumMapper.findSendState(decMsisdn, mgrId));
		}
		 commonService.createSmsOperLog("4", "定位号码", "0", "定位号码");
		checkNum = findReason(checkNum);
		log.info("check:"+checkNum.toString());
		list.add(checkNum);
		PageInfo<CheckNum> pageInfo = new PageInfo<CheckNum>(list);
		return pageInfo;
	}
	@SystemServiceLog(value = false, description = "定位项目")
	public PageInfo<CheckMgr> checkMgr(String cityId,String countyId,String mgrId,String decMsisdn,String mgrBrief,String startTime,String endTime){
		List<CheckMgr> list = new ArrayList<CheckMgr>();
		List<String> mgrIdList = new ArrayList<String>();
		if (null==mgrId || "".equals(mgrId)) {
			mgrIdList = checkNumMapper.findMgrIds(cityId,countyId,mgrBrief);
		}else {
			mgrIdList.add(mgrId);
		}
		
		for(int i=0;i<mgrIdList.size();i++){
			CheckMgr checkMgr = new CheckMgr();
			checkMgr.setCityId(cityId);
			String mgrName = checkNumMapper.findMgrName(mgrIdList.get(i));
			checkMgr.setName(mgrName);
			checkMgr.setMsisdn(decMsisdn);
			String msisdn = decMsisdn;
			if (null!=decMsisdn && !"".equals(decMsisdn)) {
				try {
					msisdn = AES.encrypt(decMsisdn, GlobalConst.SKEY);
				} catch (Exception e) {
					log.error("encrypt msisdn failed");
					e.printStackTrace();
				}
			}
			CheckNumState checkNumState = checkNumMapper.findstate(mgrIdList.get(i),mgrBrief);
			checkMgr.setState(checkNumState.getState());
			checkMgr.setTemplateState(checkNumState.getTemplateState());
			checkMgr.setIsLocal(checkNumState.getIsLocal());
			String countyName = checkNumState.getCountyName() == null ? "-" : checkNumState.getCountyName();
			checkMgr.setCountyName(countyName);
			int stationNum = checkNumMapper.findStationNum(mgrIdList.get(i));
			checkMgr.setStationNum(stationNum);	
			checkMgr.setBelong(checkMgr.getCityName()+"/"+countyName);
			
			int his = checkNumMapper.findHis(msisdn, mgrIdList.get(i));
			checkMgr.setDataSources(String.valueOf(his));
			Integer iscz = checkNumMapper.findIscz(msisdn, mgrIdList.get(i)) ;
			if (null!=iscz) {
				checkMgr.setIscz(String.valueOf(iscz));
				if (iscz == 0) {
					checkMgr.setDataSources("0");
				}
			}
			/*int seven = checkNumMapper.findSeven(msisdn, mgrId);
			checkMgr.setIsSeven(String.valueOf(seven));*/
			/*int black = checkNumMapper.findBlack(msisdn, cityId);
			checkMgr.setIsBlack(String.valueOf(black));*/
			int smSend = checkNumMapper.findSmSend(msisdn, mgrIdList.get(i));
			if (smSend==0) {
				checkMgr.setSmSendNum("0");
			} else {
				checkMgr.setSmSendNum(String.valueOf(smSend));
				checkMgr.setResult(checkNumMapper.findResult(msisdn, mgrIdList.get(i)));
			}
			int detail = checkNumMapper.findDetail(decMsisdn, mgrIdList.get(i),startTime,endTime);
			checkMgr.setDeailNum(String.valueOf(detail));
			 commonService.createSmsOperLog("4", "定位项目", "0", "定位项目");
			checkMgr = findReason2(checkMgr);
			log.info("check:"+checkMgr.toString());
			list.add(checkMgr);
		}	
		PageInfo<CheckMgr> pageInfo = new PageInfo<CheckMgr>(list);
		return pageInfo;
	}
	public CheckNum findReason(CheckNum checkNum) {
		if (!"审核通过".equals(checkNum.getTemplateStateName())) {
			checkNum.setReason("模板未通过审核");
			return checkNum;
		}
		if (0==checkNum.getStationNum()) {
			checkNum.setReason("该项目没有基站,请重新导入基站");
		}
		if ("否".equals(checkNum.getDataSources())) {
			checkNum.setReason("IT未传此条数据");
			return checkNum;
		}
		if ("是".equals(checkNum.getIsSeven())) {
			checkNum.setReason("号码是七日已发数据");
			return checkNum;
		}
		if ("1".equals(checkNum.getIsLocal()) && "是".equals(checkNum.getIscz())) {
			checkNum.setReason("该项目只发外地用户,此用户是常驻人口");
			return checkNum;
		}
		if ("是".equals(checkNum.getIsBlack())) {
			checkNum.setReason("号码是黑名单");
			return checkNum;
		}
		if ("是".equals(checkNum.getSmSend()) && -1==checkNum.getResult()) {
			checkNum.setReason("发送进程拥堵,请检查");
			return checkNum;
		}
		if ("否".equals(checkNum.getSmSend())) {
			checkNum.setReason("过滤进程拥堵,请检查");
			return checkNum;
		}
		if ("是".equals(checkNum.getDeail()) && -1==checkNum.getSendState()) {
			checkNum.setReason("发送失败");
			return checkNum;
		}
		return checkNum;

	}
	public CheckMgr findReason2(CheckMgr checkMgr) {
		if (!"审核通过".equals(checkMgr.getTemplateStateName())) {
			checkMgr.setReason("模板未通过审核");
			return checkMgr;
		}
		if (0==checkMgr.getStationNum()) {
			checkMgr.setReason("该项目没有基站,请重新导入基站");
		}
		if ("0".equals(checkMgr.getDataSources())) {
			checkMgr.setReason("IT未传此条数据");
			return checkMgr;
		}
		/*if (!"0".equals(checkMgr.getIsSeven())) {
			checkMgr.setReason("号码是七日已发数据");
			return checkMgr;
		}*/
		if ("1".equals(checkMgr.getIsLocal()) && "1".equals(checkMgr.getIscz())) {
			checkMgr.setReason("该项目只发外地用户,此用户是常驻人口");
			return checkMgr;
		}
		/*if (!"0".equals(checkMgr.getIsBlack())) {
			checkMgr.setReason("号码是黑名单");
			return checkMgr;
		}*/
		if (!"0".equals(checkMgr.getSmSendNum()) && -1==checkMgr.getResult()) {
			checkMgr.setReason("发送进程拥堵,请检查");
			return checkMgr;
		}
		if ("0".equals(checkMgr.getSmSendNum())) {
			checkMgr.setReason("过滤进程拥堵,请检查");
			return checkMgr;
		}
		if (!"0".equals(checkMgr.getDeailNum()) && -1==checkMgr.getSendState()) {
			checkMgr.setReason("发送失败");
			return checkMgr;
		}
		return checkMgr;

	}
}
